/*
 * 监控区主页的主区域, 默认, 工程播放列表页
 * 命名空间 "project-playList"
 * 
 * @author Zdream
 * @since 0.2.0
 */

monitor.ui.body.addContent({
	name : "project-playList",
	
	init : function (argv) {
		var thiz = this;
		var allPlayLists = monitor.store.queryAllPlayLists();
		
		this.state = {
			sid : argv.sid,
			allPlayLists : [],
			current : {
				index : -1,
				pid : 1,
				pname : "",
				playListId : null,
				playListSeq : null,
				playListName : null
			},
			playList : [],
			s : {
				stats : "display"
			},
			thiz : this
		};
		if (allPlayLists != null) {
			allPlayLists.forEach(item => this.state.allPlayLists.push(item));
			this.resetState();
		}
		
		// monitor.store
		// vm create
		this.vm = new Vue({
			el: "#mnt_body",
			data: this.state,
			methods: this.methods
		});

		// 消息监听
		monitor.msgCenter.subscribe(
				["aside-remove-button", "aside-refresh-button", "status-change", "query-data-project",
					"query-data-all-playList"],
		function(event, argv) {
			switch (event) {
			case "aside-remove-button":
			case "aside-refresh-button":
			case "status-change":
			{
				thiz.resetState(argv);
			} break;
			case "query-data-project":
			{
				thiz.handleQueryProject(argv);
			} break;
			case "query-data-all-playList":
			{
				thiz.handleQueryAllPlayLists(argv);
			} break;
			}
		}, this.name);
	},
	
	methods : {
		changeStats : function (stats) {
			this.thiz.changeStats(stats);
		},
		selectPlayList : function (index) {
			this.thiz.selectPlayList(index);
		},
		submitPlayList : function () {
			this.thiz.submitPlayList();
		}
	},
	
	resetState : function () {
		var status = monitor.store.screens[this.state.sid];
		var state = this.state;
		
		// current 部分
		state.current.index = status.player.current;
		if (state.current.index == -1) {
			// 正在播放列表外的工程
			state.current.pid = state.current.pname = status.pid;
			if (status.pid != null) {
				var pname = monitor.store.queryProject(status.pid).name;
				if (pname != null) {
					state.current.pname = pname;
				}
			}
		} else {
			state.current.pid = state.current.pname = null;
		}
		if (status.player.playList != null) {
			state.current.playListId = status.player.playList.lid;
			state.current.playListName = status.player.playList.name;
		}
		state.current.playListSeq = status.player.seq;
		
		// playList 部分
		state.playList.splice(0, state.playList.length); // 清空
		if (status.player.playList != null) {
			var projects = status.player.playList.projects;
			var length = projects.length;
			for (var i = 0; i < length; i++) {
				var pid = projects[i];
				var pname = pid;
				var project = monitor.store.queryProject(pid);
				if (project != null) {
					pname = project.name;
				}
				state.playList.push({
					pid : pid,
					pname : pname
				})
			}
		}
	},
	
	/**
	 * 消息触发
	 */
	handleQueryProject : function (data) {
		var pid = data.pid;
		var pname = data.name;
		
		// current
		if (this.state.current.pid == pid) {
			this.state.current.pname = pname;
		}
		
		// playList
		var array = this.state.playList;
		var length = array.length;
		for (var i = 0; i < length; i++) {
			if (array[i].pid == pid) {
				array[i].pname = pname;
			}
		}
	},
	
	/**
	 * 消息触发
	 */
	handleQueryAllPlayLists : function (data) {
		var array = this.state.allPlayLists;
		array.splice(0, array.length);
		
		data.forEach(item => array.push(item));
		this.resetState();
		this.vm.$forceUpdate();
	},
	
	/**
	 * 状态变化, 指派工作列表部分是修改状态还是查看状态
	 * 按钮触发
	 */
	changeStats : function (stats) {
		this.state.s.stats = stats;
		
		switch (stats) {
		case "display": {
			
		} break;
		case "change": {
			this.state.current.selectedPlayList = -1;
		} break;
		}
	},
	
	/**
	 * 预选择播放列表
	 * 按钮触发
	 */
	selectPlayList : function (index) {
		this.state.current.selectedPlayList = index;
		
		this.vm.$forceUpdate();
	},
	
	submitPlayList : function () {
		if (this.state.current.selectedPlayList == -1) {
			return;
		}
		
		var data = {
			sid : this.state.sid,
			playList : this.state.allPlayLists[this.state.current.selectedPlayList].lid,
			seq : null
		};
		
		monitor.web.ws.send("/playList/assign", data);
		this.changeStats("display");
	},
	
	/*
	 * 向服务器发送指派播放列表的请求
	 */
	pushAssignRequest : function () {
		// TODO
	},
	
	destroy : function () {
		this.state = null;
		
		// 删除消息监听
		monitor.msgCenter.remove(this.name);
		
		// vm destroy
		if (this.vm != null)
			this.vm.$destroy();
		this.vm = null;
	}
	
});
